//
//  TLItdViewController.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/10/12.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//

#import "TLItdViewController.h"
#import "NSArray+UIImage.h"

@interface UIScrollView (IntroductionImage)
- (void)attachIntroductionImage:(NSArray<UIImage *> *)images;
@end


@implementation UIScrollView (IntroductionImage)

- (void)attachIntroductionImage:(NSArray<UIImage *> *)images{
    for (NSUInteger i = 0; i < images.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
        
        [imageView setImage:[images objectAtIndex:i]];
        
        [imageView setTag:i];
        
        [self addSubview:imageView];
    }
}

@end

@interface TLItdViewController () <UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView   *scrollView;
@property (nonatomic,strong) UIPageControl  *pageControl;
@end

@implementation TLItdViewController
@synthesize numberOfPage,imageNames,pageControl;
-(id)initWithImageNames:(NSArray *)names flipActionBlock:(TLLtdFlipBlock)flipBlock completionBlock:(TLLtdFlipCompletionBlock)completionBlock{
    self = [super init];
    
    if(self)
    {
        numberOfPage = 4;
        
        self.flipBlock = flipBlock;
        
        self.flipCompletionBlock = completionBlock;
        
        self.imageNames = names;
        
        [self addIntrductionScrollView:numberOfPage];
        
        [self addBottomPageControlNumPage:self.numberOfPage];
    }
    
    return self;
}

- (void)viewDidLoad{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.scrollView attachIntroductionImage:[imageNames imageNamesToImage]];
}


#pragma mark -- Views
/**
 *  Add Introduction ScrollView
 */

- (void)addIntrductionScrollView:(NSUInteger)page{
    //Scroll View Content Size
    CGSize scrollViewContentSize = CGSizeMake([TLDeviceManger deviceScreenSize].width * page, [TLDeviceManger deviceScreenSize].height);
    
    self.scrollView = [self introductionScrollView:scrollViewContentSize];
    
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    
    self.scrollView.backgroundColor = [UIColor clearColor];
    
    [self.scrollView setDelegate:self];
    
    [self.scrollView setPagingEnabled:YES];
    
    [self.view addSubview:self.scrollView];
}


- (void)addBottomPageControlNumPage:(NSUInteger)num{
    //Bottom
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, [TLDeviceManger deviceScreenSize].width/3, 40)];
    
    [pageControl setNumberOfPages:num];
    
    pageControl.translatesAutoresizingMaskIntoConstraints = NO;
    
    [pageControl setBackgroundColor:[UIColor clearColor]];
    
    [self.view addSubview:pageControl];
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[pageControl]-20-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(pageControl)]];
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[pageControl]-100-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(pageControl)]];
    
     //[pageControl addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[pageControl]-30-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(pageControl)]];
}

- (UIScrollView *)introductionScrollView:(CGSize)contentSize{
    //ScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [TLDeviceManger deviceScreenSize].width, [TLDeviceManger deviceScreenSize].height)];
    [scrollView setContentSize:contentSize];
    
    return scrollView;
}




#pragma mark -- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(nonnull UIScrollView *)scrollView{
    if(scrollView.contentOffset.x >= (scrollView.contentSize.width - CGRectGetWidth(scrollView.frame)))
    {
        //滑到头
        self.flipCompletionBlock();
    }
    
    NSLog(@"%lf %lf",scrollView.contentOffset.x,(scrollView.contentSize.width - CGRectGetWidth(scrollView.frame)));
}

- (void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView{
    //the current page
    [self.pageControl setCurrentPage:(scrollView.contentOffset.x/scrollView.frame.size.width)];
    
    self.flipBlock(self.pageControl.currentPage);
}


@end
